package cn.dinkevin.xui.watcher;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

import cn.dinkevin.xui.util.RegularUtil;

/**
 * 手机号码格式化处理</br>
 * 处理后格式:185 1252 5723
 * Created by ChengPengFei on 2017/2/13 0013.</br>
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public class MobileFormatTextWatcher implements TextWatcher{

    int beforeTextLength = 0;
    int onTextLength = 0;
    boolean isChanged = false;

    int location = 0;           // 记录光标的位置
    private char[] tempChar;
    private StringBuffer buffer = new StringBuffer();
    int numberB = 0;
    private EditText edt_input;

    private char SPECIAL_CHAR = ' ';    // 默认的分隔符

    /**
     * 声明手机号码输入结束监听
     */
    public interface InputListener {

        /**
         * 手机号码输入监听
         * @param inputContent 输入的内容
         * @param isMobile 是否为手机号
         */
        void onInputChanged(String inputContent, boolean isMobile);
    }
    protected InputListener mCallback;
    protected final int MAX_INPUT_NUMBER = 13;

    public MobileFormatTextWatcher(EditText edt_input, InputListener callback){
        this.edt_input = edt_input;
        this.edt_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_INPUT_NUMBER)});
        this.mCallback = callback;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        beforeTextLength = s.length();
        if (buffer.length() > 0) {
            buffer.delete(0, buffer.length());
        }
        numberB = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == SPECIAL_CHAR) {
                numberB++;
            }
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        onTextLength = s.length();
        buffer.append(s.toString());
        if (onTextLength == beforeTextLength || onTextLength <= 3
                || isChanged) {
            isChanged = false;
            return;
        }
        isChanged = true;
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (isChanged) {
            location = edt_input.getSelectionEnd();
            int index = 0;
            while (index < buffer.length()) {
                if (buffer.charAt(index) == ' ') {
                    buffer.deleteCharAt(index);
                } else {
                    index++;
                }
            }

            index = 0;
            int numberC = 0;
            while (index < buffer.length()) {
                if ((index == 3 || index == 8)) {
                    buffer.insert(index, SPECIAL_CHAR);
                    numberC++;
                }
                index++;
            }

            if (numberC > numberB) {
                location += (numberC - numberB);
            }

            tempChar = new char[buffer.length()];
            buffer.getChars(0, buffer.length(), tempChar, 0);
            String str = buffer.toString();
            if (location > str.length()) {
                location = str.length();
            } else if (location < 0) {
                location = 0;
            }

            edt_input.setText(str);
            if(location >= MAX_INPUT_NUMBER){
                location = MAX_INPUT_NUMBER;
            }
            Editable editable = edt_input.getText();
            Selection.setSelection(editable, location);
            isChanged = false;

            String phoneNumber = buffer.toString().replaceAll(SPECIAL_CHAR + "","");
            if(null != mCallback){
                mCallback.onInputChanged(phoneNumber, RegularUtil.isMobile(phoneNumber));
            }
        }
    }
}
